/** @format */

import { defineConfig } from 'vite';

import viteBaseConfig from './vite.base.config.ts';
import viteDevConfig from './vite.dev.config.ts';
import viteTestConfig from './vite.dev.config.ts';
import viteProdConfig from './vite.prod.config.ts';

const envResolver: any = {
  serve: () => {
    console.log('本地环境');
    return { ...viteBaseConfig, ...viteDevConfig };
  },
  test: () => {
    console.log('测试环境');
    return { ...viteBaseConfig, ...viteTestConfig };
  },
  build: () => {
    console.log('生产环境');
    return { ...viteBaseConfig, ...viteProdConfig };
  },
};

export default defineConfig(({ command, mode }: any) => {
  console.log('command,mode ===>', command, mode);
  // 加载环境变量，确保环境变量文件路径正确
  // const env = loadEnv(mode, `${process.cwd()}/config/env`, 'VITE_');
  // const env = loadEnv(mode, process.cwd(), 'VITE_');
  // console.log('env.VITE_APP_KEY ===>', env.VITE_APP_KEY);

  // 指定新的 env 目录路径
  // const envDir = './config/env';
  var theEnvResolver: any = {};
  if (mode == 'test') {
    theEnvResolver = { ...envResolver[mode]() };
  } else {
    theEnvResolver = { ...envResolver[command]() };
  }
  return {
    // envDir,
    // plugins: [  // 因为这里有vite.base.config.ts，所以这里 应该是被 ...envResolver[command]() 解析的vite.base.config.ts 文件覆盖掉了
    //   vue(),
    // ],
    ...theEnvResolver,
  };
});
